using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Webadmin_Modules_User_UserInfo : System.Web.UI.UserControl
{
    //----------------------------------------------------------
    public DataRow _SiteOption = null;
    public DataRow _AdminLang = null;
    public string _Act = "";
    private string _SiteNote = "General";
    private string _LangNote = "UserList";
    public string _Filter = "";
    public string _Navigator = "";
    public string _ID = "";
    public string _CateID = "";
    private void initSiteLang()
    {
        
        if (!Request.Url.AbsoluteUri.Contains(Session["Page"].ToString()))
        {
            Session["Page"] = Request.Url.AbsoluteUri.Split('?')[0];
        }
        //---------
        Session["Lang"] = Request.QueryString["Lang"] == null ? Session["Lang"] : Request.QueryString["Lang"];
        Session["Lang"] = bzAdminLang.checkLang(Session["Lang"].ToString());
        _SiteOption = new bzSiteOption("SiteAdmin", _SiteNote).SiteOption;
        _AdminLang = new bzAdminLang(Session["Lang"].ToString(), _LangNote).AdminLang;

        
        _Act = Request.QueryString["Act"] == null ? "" : Request.QueryString["Act"];
        _ID = Request.QueryString["ID"] == null ? "-1" : Request.QueryString["ID"];
        _CateID = Request.QueryString["CateID"] == null ? "-1" : Request.QueryString["CateID"];

        this.loadAction();
    }

    //----------------------------------------------------------------------------------



    private DataRow _dr =  null;
    private bzAdminUser bz = new bzAdminUser();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.initSiteLang();
    }
    private void loadAction()
    {   DataTable  dt = bz.getRecord(Session["AdminUser"].ToString(), "1");
       if (dt.Rows.Count > 0) 
       {_dr = dt.Rows[0];}
       if (_dr["Enabled"].ToString() == "0") Response.Redirect("UserList.aspx");
        if (!IsPostBack)
        {
            this.loadCate();
            this.loadInfo();
        }
    }
    private void loadInfo()
    {
        if (_dr == null) Response.Redirect("UserList.aspx");      
       this.txtAdminUser.Text = _dr["AdminUser"].ToString();
       this.txtEmail.Text = _dr["Email"].ToString();
       this.cboCategory.SelectedValue = _dr["GroupUser"].ToString();
       
       this.btnUpdate.Text = _AdminLang["Edit"].ToString();
       this.rdEnabled_1.Checked = true;
       this.rdEnabled_0.Checked = false;
    }
    private void loadCate()
    {
        DataTable dtCate = new bzUserGroup().getTable("", "1", "-1", "-1");
        this.cboCategory.DataSource = dtCate;
        this.cboCategory.DataTextField = "GroupUser";
        this.cboCategory.DataValueField = "GroupUser";
        this.cboCategory.DataBind();
        this.cboCategory.Items.Insert(0, new ListItem(_AdminLang["All"].ToString(), "-1"));

        this.cboCategory.SelectedValue = _CateID;
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (_dr == null) Response.Redirect("UserList.aspx");      
        if (bzStringClass.MD5(this.txtPasswordCurrent.Text) == _dr["AdminPassword"].ToString())
        {
            string pass = "";
            if (this.txtPassword.Text == "")
            {                
                pass = _dr["AdminPassword"].ToString();
            }
            else pass = bzStringClass.MD5(this.txtPassword.Text);
            if (bz.updateRecord(this.txtAdminUser.Text, this.txtEmail.Text, pass, this.rdEnabled_0.Checked ? "0" : "1", this.cboCategory.SelectedValue) > 0)
            {
                Response.Write(bzStringClass.addStringAlert(_AdminLang["Successful"]));
                //Response.Redirect(Session["Page"].ToString() + "?CateID=" + this.cboCategory.SelectedValue);
            }

        }
        else 
        {
            Response.Write(bzStringClass.addStringAlert(_AdminLang["UserList_PasswordWrong"]));
        }
    }
   
}
